package com.ccn.crm.interfaces.impl;

import com.ccn.crm.model.Product;
import com.ccn.crm.model.User;
import com.ccn.crm.model.installManage;
import com.ccn.crm.model.installManageDetail;
import com.ccn.crm.service.IInstall;
import com.ccn.crm.service.IInstallDetail;
import com.ccn.crm.service.IProduct;
import com.ccn.crm.service.IUser;
import com.ccn.crm.util.*;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.*;

@Service
public class PushInstallService implements IPushInstall {

    private static final Logger LOG = LoggerFactory.getLogger(PushInstallService.class);
    @Autowired
    private IProduct iProduct;
    @Autowired
    private IUser iUser;
    @Autowired
    private IInstall iInstall;
    @Autowired
    private IInstallDetail iInstallDetail;
    @Autowired
    private EmailUtil emailUtil;
    @Override
    public void pushDate()  throws ParseException {
        LOG.info("开始想crm推送实施数明细据");
        Map<String, Object> token = GetToken.getToken();
        List<installManage> installManages = iInstall.selectAll();

        if (installManages != null && installManages.size() != 0) {
            for (installManage installManage : installManages) {

                Map<String,Object> dateObject = new HashMap<>();
                //获取没有完成的项目
                Integer id = installManage.getId();
                //通过项目实施查询实施明细填写的项目
                List<installManageDetail> installManageDetails = iInstallDetail.selectByInstallId(id);
                if (installManageDetails!=null&&installManageDetails.size()!=0) {
                    for (installManageDetail installManageDetai :installManageDetails ) {
                        if (null==installManageDetai.getInstalluser()||installManageDetai.getInstalluser().equals(""))
                        {
                            continue;
                        }
                        System.out.println("根据实施id 查询下列明细");
                        //本次实际进场时间	Entrydate	field_hDhcu__c
                        //本次离场时间	departuredate	field_yVaw1__c
                        //实际发生差旅费用	travel	field_K2CpO__c
                        //收取客户差旅费用	supplier	field_n9yNl__c
                        //人天成本	labourcost	field_8qAIW__c
                        //实施说明	travel	field_Rc72v__c
                        //实施人员	Installuser	field_3bDIf__c
                        //是否外包	status	field_GGvIk__c
                        //外包供应商	supplier	field_kJmH5__c

                        //获取crm明细表id  查出该条记录是否已经存在

                        String crmId = installManageDetai.getCrmId();
                        if (null!=crmId&&crmId.equals("")) {
                            Map<String, Object> installDetailInfoById = CreateInstallManageDetail.getInstallDetailInfoById(crmId, token);

                        System.out.println(installDetailInfoById);
                        String errorMess = (String) installDetailInfoById.get("errorMessage");

                        if (!errorMess.equals("OK")) {
                            if (null!=installManageDetai.getDeparturedate()) {
                                long editeddate = installManageDetai.getDeparturedate().getTime();
                                dateObject.put("field_hDhcu__c", editeddate);
                            }
                            if (null!=installManageDetai.getEntrydate()) {
                                long entrydate = installManageDetai.getEntrydate().getTime();
//                            System.out.println(editeddate);
//                            System.out.println(entrydate);

                                dateObject.put("field_yVaw1__c", entrydate);
                            }
//                        Integer  timeDiff= DateUtil.daysBetween(installManageDetai.getDeparturedate(),installManageDetai.getEntrydate());
//                            if (timeDiff==0){
//                                timeDiff=1;
//                            }
                            String installDay = installManageDetai.getInstallDay();
                            if (null!=installDay&&!installDay.equals("")) {
                                Double timeDiff = Double.valueOf(installDay);
                                dateObject.put("field_415cv__c", timeDiff);
                            }
                            //实际发生差旅费用	travel	field_K2CpO__c
                            String travel = installManageDetai.getTravel();

                            if (null != travel&&!travel.equals("")) {
                                Double trave = Double.valueOf(travel);
                                dateObject.put("field_K2CpO__c", trave);
                            }
                            //收取客户差旅费用		field_n9yNl__c
                            String csupplier = installManageDetai.getCollecttravel();

                            if (null != csupplier&&!csupplier.equals("")) {
                                Double csupplie = Double.valueOf(csupplier);
                                dateObject.put("field_n9yNl__c", csupplie);
                            }
                            //人天成本
                            String labourcost = installManageDetai.getLabourcost();
                            if (null != labourcost&&!labourcost.equals("")) {
                                Double labourcos = Double.valueOf(labourcost);
                                dateObject.put("field_8qAIW__c", labourcos);
                            }
                            //实施说明instructions
                            String instructions = installManageDetai.getInstructions();
                            if (null != instructions!=instructions.equals("")) {
                                dateObject.put("field_Rc72v__c", instructions);
                            }
                            //实施人员
                            String installuser = installManageDetai.getInstalluser();
                            if (null!=installuser&&!installuser.equals("")) {
                                User installHeader = iUser.selectByAccount(installuser);
                                ArrayList<Object> installHeaderObjects = new ArrayList<>();
                                installHeaderObjects.add(installHeader.getRealname());
                                String installHeaderuser = GetUserInfo.GetUserId(installHeaderObjects, token);
                                if (installHeaderuser == null) {
                                    LOG.info("获取实施负责人信息失败，跳过该项目:" + installManage.getPcode());

                                } else {

                                    ArrayList<Object> installHeadId = new ArrayList<>();
                                    installHeadId.add(installHeaderuser);
                                    dateObject.put("field_3bDIf__c", installHeadId);
                                }
//                            }
//
                                ArrayList<Object> owner = new ArrayList<>();
                                owner.add(installHeaderuser);
                                dateObject.put("owner", owner);
                            }
                            String status = installManageDetai.getStatus();
                            if (null != status&&!status.equals("")) {
                                if (status.equals("1")) {
                                    dateObject.put("field_GGvIk__c", "option1");
                                } else if (status.equals("2")) {
                                    dateObject.put("field_GGvIk__c", "tzme5zm6B");
                                } else {
                                    dateObject.put("field_GGvIk__c", "other");
                                }
                            }
                            String supplier = installManageDetai.getSupplier();
                            if (null != supplier&&!supplier.equals("")) {
                                if (supplier.equals("1")){
                                    dateObject.put("field_kJmH5__c", "b0m46sf7n");
                                }else if (supplier.equals("2")){
                                    dateObject.put("field_kJmH5__c", "cd36hhb8U");
                                }else if (supplier.equals("3")){
                                    dateObject.put("field_kJmH5__c", "102oyNsp5");
                                }else if (supplier.equals("4")){
                                    dateObject.put("field_kJmH5__c", "WF8Bq8xqd");
                                }else if (supplier.equals("5")){
                                    dateObject.put("field_kJmH5__c", "5a424t7Lz");
                                }else if (supplier.equals("6")){
                                    dateObject.put("field_kJmH5__c", "ccooiPw3K");
                                }else if (supplier.equals("7")){
                                    dateObject.put("field_kJmH5__c", "Au0Wm0eh2");
                                }else if (supplier.equals("8")){
                                    dateObject.put("field_kJmH5__c", "iBHaZer9z");
                            }else{
                                    dateObject.put("field_kJmH5__c", "other");
                                }
                            }

                            dateObject.put("record_type","record_fT226__c");


                            String crmIdProject = installManage.getCrmId();

                            dateObject.put("field_ukxLZ__c", crmIdProject);

                            dateObject.put("dataObjectApiName","object_0p04E__c");
                            Map<String, Object> installDetail = CreateInstallManageDetail.createInstallDetail(token, dateObject);
                            System.out.println(installDetail);
                            String errorMessage = (String) installDetail.get("errorMessage");
                            if (errorMessage.equals("OK")) {
                                LOG.info("向crm创建实施明细成功:" + installManage.getPcode());
                                //添加成功后向明细表添加创建的crmid
                                String dataId = installDetail.get("dataId").toString();
                                int i = iInstallDetail.updateInstallDetailCrmId(installManageDetai.getId(), dataId);
                                if (i==1){
                                    LOG.info("向实施明细表添加crmid成功:" + installManage.getPcode());
                                }else {
                                    LOG.info("向实施明细表添加crmid失败:" + installManage.getPcode());
                                }

                            } else {
                                emailUtil.sendMail("向crm创建实施明细失败--"+errorMessage);
                                LOG.info("向crm创建实施明细失败" + installManage.getPcode() + "--crm返回失败原因：" + errorMessage);
                            }
                        }else {
                            if (null!=installManageDetai.getDeparturedate()) {
                                long editeddate = installManageDetai.getDeparturedate().getTime();
                                dateObject.put("field_hDhcu__c", editeddate);
                            }
                            if (null!=installManageDetai.getEntrydate()) {
                                long entrydate = installManageDetai.getEntrydate().getTime();
                                dateObject.put("field_yVaw1__c", entrydate);
                            }


//                            Integer  timeDiff= DateUtil.daysBetween(installManageDetai.getDeparturedate(),installManageDetai.getEntrydate());
//                            if (timeDiff==0){
//                                timeDiff=1;
//                            }
                            String installDay = installManageDetai.getInstallDay();
                            if (null!=installDay&&!installDay.equals("")) {
                                Double timeDiff = Double.valueOf(installDay);
                                dateObject.put("field_415cv__c", timeDiff);
                            }
                            //实际发生差旅费用	travel	field_K2CpO__c
                            String travel = installManageDetai.getTravel();

                            if (null != travel&&!travel.equals("")) {
                                Double trave = Double.valueOf(travel);
                                dateObject.put("field_K2CpO__c", trave);
                            }
                            //收取客户差旅费用		field_n9yNl__c
                            String csupplier = installManageDetai.getCollecttravel();

                            if (null != csupplier&&!csupplier.equals("")) {
                                Double csupplie = Double.valueOf(csupplier);
                                dateObject.put("field_n9yNl__c", csupplie);
                            }
                            //人天成本
                            String labourcost = installManageDetai.getLabourcost();
                            if (null != labourcost) {
                                Double labourcos = Double.valueOf(labourcost);
                                dateObject.put("field_8qAIW__c", labourcos);
                            }
                            //实施说明instructions
                            String instructions = installManageDetai.getInstructions();
                            if (null != instructions) {
                                dateObject.put("field_Rc72v__c", instructions);
                            }
                            //实施人员
                            String installuser = installManageDetai.getInstalluser();
                            if (null!=installuser&&installuser.equals("")) {
                                User installHeader = iUser.selectByAccount(installuser);
                                ArrayList<Object> installHeaderObjects = new ArrayList<>();
                                installHeaderObjects.add(installHeader.getRealname());
                                String installHeaderuser = GetUserInfo.GetUserId(installHeaderObjects, token);
                                if (installHeaderuser == null) {
                                    LOG.info("获取实施负责人信息失败，跳过该项目:" + installManage.getPcode());

                                } else {

                                    ArrayList<Object> installHeadId = new ArrayList<>();
                                    installHeadId.add(installHeaderuser);
                                    dateObject.put("field_3bDIf__c", installHeadId);
                                }
                                ArrayList<Object> owner = new ArrayList<>();
                                owner.add(installHeaderuser);
                                dateObject.put("owner", owner);
                            }

                            String status = installManageDetai.getStatus();
                            if (null != status&&!status.equals("")) {
                                if (status.equals("1")) {
                                    dateObject.put("field_GGvIk__c", "option1");
                                } else if (status.equals("2")) {
                                    dateObject.put("field_GGvIk__c", "tzme5zm6B");
                                } else {
                                    dateObject.put("field_GGvIk__c", "other");
                                }
                            }
                            String supplier = installManageDetai.getSupplier();
                            if (null != supplier&&!supplier.equals("")) {
                                if (supplier.equals("1")){
                                    dateObject.put("field_kJmH5__c", "b0m46sf7n");
                                }else if (supplier.equals("2")){
                                    dateObject.put("field_kJmH5__c", "cd36hhb8U");
                                }else if (supplier.equals("3")){
                                    dateObject.put("field_kJmH5__c", "102oyNsp5");
                                }else if (supplier.equals("4")){
                                    dateObject.put("field_kJmH5__c", "WF8Bq8xqd");
                                }else if (supplier.equals("5")){
                                    dateObject.put("field_kJmH5__c", "5a424t7Lz");
                                }else if (supplier.equals("6")){
                                    dateObject.put("field_kJmH5__c", "ccooiPw3K");
                                }else if (supplier.equals("7")){
                                    dateObject.put("field_kJmH5__c", "Au0Wm0eh2");
                                }else{
                                    dateObject.put("field_kJmH5__c", "other");
                                }
                            }
                            //

                            dateObject.put("record_type", "record_fT226__c");

                            String crmIdProject = installManage.getCrmId();

                            dateObject.put("field_ukxLZ__c", crmIdProject);
                            dateObject.put("_id", crmId);
                            dateObject.put("dataObjectApiName", "object_0p04E__c");

                            Map<String, Object> installDetail = CreateInstallManageDetail.updateIntallDetail(dateObject, token);

                            String errorMessage = (String) installDetail.get("errorMessage");
                            System.out.println(errorMessage);
                            if (errorMessage.equals("OK")) {
                                LOG.info("向crm修改实施明细成功:" + installManage.getPcode());
                            } else {
                                emailUtil.sendMail("向crm修改实施明细失败:"+ installManage.getPcode() +"--"+errorMessage);
                                LOG.info("向crm修改实施明细失败" + installManage.getPcode() + "--crm返回失败原因：" + errorMessage);
                            }
                        }
                    }
                }

                String status = installManage.getStatus();
                if (status.equals("3")){
                    int i = iInstall.updateStatusById(id, "2");
                    if (i==1){
                        LOG.info("修改实施表推送状态成功");
                    }else {
                        LOG.info("修改实施表推送状态失败");
                    }
                }

            }
        }
    }
 }
}
